package proj.pt.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import proj.pt.services.LogicProcessor;

public class CustomSimpleFormController extends SimpleFormController {

	private LogicProcessor logicProcessor;
	private Navigation leftNav;
	private String leftNavSelect;
	private String successURI;
	private String failureURI;
	
	private List accessRoles = new ArrayList();
	
	public String getFailureURI() {
		return failureURI;
	}
	public void setFailureURI(String failureURI) {
		this.failureURI = failureURI;
	}
	public LogicProcessor getLogicProcessor() {
		return logicProcessor;
	}
	public void setLogicProcessor(LogicProcessor logicProcessor) {
		this.logicProcessor = logicProcessor;
	}
	public Navigation getLeftNav() {
		return leftNav;
	}
	public void setLeftNav(Navigation leftNav) {
		this.leftNav = leftNav;
	}
	public String getLeftNavSelect() {
		return leftNavSelect;
	}
	public void setLeftNavSelect(String leftNavSelect) {
		this.leftNavSelect = leftNavSelect;
	}
	public String getSuccessURI() {
		return successURI;
	}
	public void setSuccessURI(String successURI) {
		this.successURI = successURI;
	}
	
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response)
     throws Exception {
		// get the User role
		String currentUserRole = (String)request.getSession().getAttribute("LOGIN_USER_ROLE");
		
		String servletPath = request.getServletPath();
		System.out.println("[servletPath] " + servletPath);
		// get the target view page from Spring 
		ModelAndView  mav = super.handleRequestInternal(request, response);
		
		if(servletPath.indexOf("logout.pt") != -1 || servletPath.indexOf("login.pt") != -1){
			return mav;
		} 
		else if(!StringUtils.hasLength(currentUserRole)){
			// since no role, logging out
			mav.setView(new RedirectView("logout.pt"));
		} 		
		// checking for privilege from current role of user to view current page
		else if((servletPath.indexOf("home.pt") == -1) && !accessRoles.contains(currentUserRole)){
			// since no privilege, showing the home page
			mav.setView(new RedirectView("home.pt"));
		} //end if
		
		return mav;
	}
	
	protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
		Map dataMap = new HashMap();
		dataMap.put("leftNav", getLeftNav());
		dataMap.put("leftNavSelect", getLeftNavSelect());
		dataMap.put("user", request.getSession().getAttribute("LOGIN_USER_NAME"));
		String userRole = (String)request.getSession().getAttribute("LOGIN_USER_ROLE");
		dataMap.put("userRole", userRole);
		dataMap.put("currentUser", request.getSession().getAttribute("LOGIN_USER"));
		
		if(userRole != null)
		dataMap.put("canAddOrUpdate", (userRole.indexOf("_FULL_ACCESS") != -1)?Boolean.TRUE:Boolean.FALSE);
		
		
		return dataMap;
	} //end referenceData
	public List getAccessRoles() {
		return accessRoles;
	}
	public void setAccessRoles(List accessRoles) {
		this.accessRoles = accessRoles;
	}
	
	/**
	 * This method helps to bypass the onBindAndValidate() method, so a return value true
	 * won't invoke the validation. 
	 */
	protected boolean doValidate(HttpServletRequest request){
		String action = WebUtils.getAction(request);
		if("action_cancel".equals(action) || "action_course_select".equals(action)
				|| "action_add".equals(action) || "action_remove".equals(action) 
				|| "action_semester_select".equals(action) || "action_select".equals(action)
				|| "action_type_select".equals(action) || "action_examtype_select".equals(action)
				|| "action_report_type_select".equals(action)){
				return false;
		} //end if
		
		return true;
	}
	
	/**
	 * This method is called by spring, which decides to show the same page or to go with
	 * submit process.
	 */
	protected boolean isFormChangeRequest(HttpServletRequest request, Object command) {
		
		String action = WebUtils.getAction(request);		
		if("action_add".equals(action) || "action_remove".equals(action) 
				|| "action_course_select".equals(action) || "action_semester_select".equals(action) 
				|| "action_select".equals(action) || "action_show".equals(action)
				|| "action_type_select".equals(action) || "action_examtype_select".equals(action)
				|| "action_report_type_select".equals(action)){
			return true;
		}
		
		return false;
	}
	
	
}
